package com.dnjn.happiness.system.controller;

import com.dnjn.happiness.biz.api.RemoteCommunicateService;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.core.web.page.TableDataInfo;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.InnerAuth;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.system.domain.SysConfig;
import com.dnjn.happiness.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author happy
 */
@RestController
@RequestMapping("/config")
public class SysConfigController extends BaseController {
    @Autowired
    private ISysConfigService configService;

    @Autowired
    private RemoteCommunicateService communicateService;

    /**
     * 根据参数键名查询参数值
     */
    @InnerAuth
    @GetMapping(value = "/configKeyApi/{configKey}")
    public R<String> getConfigKeyApi(@PathVariable String configKey) {
        return R.ok(configService.selectConfigByKey(configKey));
    }

    //小程序
    @InnerAuth
    @GetMapping("/getThresholdDataApi")
    public R<String> getThresholdDataApi(){
        String key = configService.getConfigValueByKey("biz.happinessScore.threshold");
        return R.ok(key);
    }

    /**
     * 获取参数配置列表
     */
    @RequiresPermissions("system:config:list")
    @GetMapping("/list")
    public TableDataInfo list(SysConfig config) {
        startPage();
        List<SysConfig> list = configService.selectConfigList(config);
        return getDataTable(list);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:config:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfig config) {
        List<SysConfig> list = configService.selectConfigList(config);
        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
        util.exportExcel(response, list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @RequiresPermissions("system:config:query")
    @GetMapping(value = "/{configId}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getInfo(@PathVariable Long configId) {
        return success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getConfigKey(@PathVariable String configKey) {
        return success(configService.selectConfigByKey(configKey));
    }


    /**
     * 新增参数配置
     */
    @RequiresPermissions("system:config:add")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(SecurityUtils.getUsername());
        return toAjax(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @RequiresPermissions("system:config:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(configService.updateConfig(config));
    }


    /**
     * 删除参数配置
     */
    @RequiresPermissions("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds) {
        configService.deleteConfigByIds(configIds);
        return success();
    }

    /**
     * 刷新参数缓存
     */
    @RequiresPermissions("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache() {
        configService.resetConfigCache();
        return success();
    }

    /**
     * 快乐值监控临界值
     * @return
     */
    @RequiresPermissions("system:config:query")
    @GetMapping("/getThresholdDataAdmin")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getThresholdDataAdmin() {
        String key = configService.getConfigValueByKey("biz.happinessScore.threshold");
        return new AjaxResult().put("key", key);
    }

    //小程序
    @GetMapping("/getThresholdData")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getThresholdData() {
        String key = configService.getConfigValueByKey("biz.happinessScore.threshold");
        return new AjaxResult().put("key", key);
    }


    /**
     * 修改快乐值监控临界值
     * @return
     */
    @GetMapping("/editThresholdData/{value}")
    @RequiresPermissions("system:config:query")
    public AjaxResult editThresholdData(@PathVariable String value) {
        String key = "biz.happinessScore.threshold";
        configService.updateConfigByKey(key, value);
        //走定时插入沟通表逻辑，将状态为0的删除，再新增需沟通记录
        R<Boolean> booleanR = communicateService.deleteByStatus(value, SecurityConstants.INNER);
        return AjaxResult.success();
    }


    /**
     * 获取开启快乐豆功能权限
     * @return
     */
    @GetMapping("/getHappinessBeanData")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getHappinessBeanData() {
        String key = configService.getConfigValueByKey("biz.happinessBean.enabled");
        return new AjaxResult().put("key", key);
    }
    /**
     * 修改开启快乐豆功能权限
     * @return
     */
    @GetMapping("/editHappinessBeanData/{value}")
    public AjaxResult editHappinessBeanData(@PathVariable String value) {
        String key = "biz.happinessBean.enabled";
        configService.updateConfigByKey(key, value);
        return AjaxResult.success();
    }


    /**
     * 获取开启下级对上级评价功能
     *
     * @return
     */
    @GetMapping("/getSuperiorData")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getSuperiorData() {
        String key = configService.getConfigValueByKey("biz.appraise.superior.enabled");
        return new AjaxResult().put("key", key);
    }

    /**
     * 修改开启下级对上级评价功能
     *
     * @return
     */
    @GetMapping("/editSuperiorData/{value}")
    public AjaxResult editSuperiorData(@PathVariable String value) {
        String key = "biz.appraise.superior.enabled";
        configService.updateConfigByKey(key, value);
        return AjaxResult.success();
    }

}
